package cn.ajb.filemanager.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ajb.filemanager.entity.FileManager;
import cn.ajb.filemanager.entity.User;
import cn.ajb.filemanager.service.FileManagerService;
import cn.ajb.filemanager.util.Contant;
import cn.ajb.filemanager.util.PageModel;

@Controller
@RequestMapping(value="/qr")
public class QrController {
	@Autowired
	private FileManagerService fileManagerService;
	@Autowired
	private Contant contant;
	//终端用户来访问的页面
	/*@RequestMapping(value="/qrFiledowloand")
	public String qrDowloandFileView(Integer id,HttpServletRequest request ,HttpServletResponse response,ModelMap model){
		//根据ID来获取file文件
		try {
			List<FileManager> fileManagers = fileManagerService.selectFileManagerListByParentId(id,request);
			model.put("fileManagers", fileManagers);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		return "qrUI";
	}*/
	@RequestMapping(value="/qrFiledowloand")
	public String qrDowloandFileView( @RequestParam(required = true) Integer id,Integer pageNo,Integer pageSize,HttpServletRequest request ,HttpServletResponse response,ModelMap model) throws Exception{
		//根据ID来获取file文件 @RequestParam(required = true) Integer parentId ,Integer pageNo,Integer pageSize,HttpServletRequest request
		/*try {
			List<FileManager> fileManagers = fileManagerService.selectFileManagerListByParentId(id,request);
			model.put("fileManagers", fileManagers);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}*/
		if(pageNo==null){
			pageNo = 1;
		}
		if(pageSize==null){
			pageSize = 10;
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("parentId", id);
		PageModel<FileManager> pageModel = fileManagerService.selectFileManagerListByParentIdAndPage(paramMap ,pageNo ,pageSize,request);
		List<FileManager> list = fileManagerService.selectFileManagerListById(new ArrayList<FileManager>(),id);
		model.put("pageModel", pageModel);
		model.put("breadcrumbList", list);
		model.put("id", id);
		request.getSession().setAttribute("SYS_USER", new User());
		return "qrUI";
	}
	
	@RequestMapping("/getFileManagerListByParentId")
	public String getFileManagerListByParentId(Integer parentId ,Integer pageNo,Integer pageSize,HttpServletRequest request,ModelMap modelMap) throws Exception{
		if(pageNo==null){
			pageNo = 1;
		}
		if(pageSize==null){
			pageSize = contant.getPageSize();
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("parentId", parentId);
		
		PageModel<FileManager> pageModel = fileManagerService.selectFileManagerListByParentIdAndPage(paramMap,pageNo ,pageSize,request);
		List<FileManager> list = fileManagerService.selectFileManagerListById(new ArrayList<FileManager>(),parentId);
		modelMap.put("pageModel", pageModel);
		modelMap.put("breadcrumbList", list);
		return "qrUI";
	}
}
